
export interface IBaseApi {
  url: string;
  // GM_xmlhttpRequest method type，unsupport PUT DELETE
  method: 'GET' | 'HEAD' | 'POST' | undefined;
  body: any;
}
const BaseApi = ({ url, method, body }: IBaseApi): Promise<responseData> => {
  return new Promise((resolve, reject) => {
    // console.log(process.env.API_URL + url, body);

    GM_xmlhttpRequest({
      method,
      url: process.env.API_URL + url,
      headers: {
        "Content-Type": "application/json",
      },
      data: JSON.stringify(body),
      onload: function (response) {
        if (response.status !== 200) {
          // console.log(`response status not 200,`, response);
          reject(response.statusText);
        }
        resolve(response.statusText);
      },
      onerror: function (response) {
        // console.log(`response error`, response);
        reject(response.statusText);
      }
    })
  });
}
/**
 * 脚本目前只支持 GET POST
 */
export const get = (url: string, data: requestBody): Promise<responseData> => {
  return BaseApi({ url, method: 'GET', body: data });
}
export const post = (url: string, data: requestBody): Promise<responseData> => {
  return BaseApi({ url, method: 'POST', body: data });
}